<?php

namespace app\admin\model;

use app\common\model\Config;
use think\Model;
use think\Cache;
use think\Env;

class Sale extends Model
{
    // 表名
    protected $name = 'sale';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'grade_text',
        'uptime_text',
        'endtime_text',
        'region_text',
    ];
    

    
    public function getGradeList()
    {
        return ['1' => __('高考'),'2' => __('高一'),'3' => __('中考'),'4' => __('初一'),'5' => __('考题开讲')];
    }

    public function getRegionList()
    {
        return ['14' => __('东区'),'15' => __('北区'),'16' => __('西区'),'17' => __('南区')];
    }

    public function getRegionTextAttr($value, $data)
    {
        $value = $value ? $value : $data['region'];
        $list = $this->getRegionList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getGradeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['grade'];
        $list = $this->getGradeList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    public function getUptimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['uptime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['endtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setUptimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public static function config()
    {
        $config = [
            'agent_id' => Env::get('sale.agent_id'),
            'corp_id' => Env::get('sale.corp_id'),
            'secret'   => Env::get('sale.secret'),
            'token'   => Env::get('sale.token'),
            'aes_key'   => Env::get('sale.aes_key'),
            'response_type' => Env::get('sale.response_type'),
            'log' => [
                'level' => Env::get('sale.level'),
                'file' => __DIR__.Env::get('sale.file'),
            ],
            'oauth'=> [
                'scopes'=> Env::get('sale.scopes'),
                'callback' => Env::get('sale.callback','/')
            ]
        ];
        return $config;
    }

    public function ApiAdd($params,$user,$region,$upTime,$endTime)
    {
        $video = $this->StringMerging($params['video']);
        $params['video_num'] = $video['num'] + $params['office_total'] + $params['book_total'];
        $params['video'] = serialize($params['video']);
        $book = $this->StringMerging($params['book'],2);
        $params['book_num'] = $book['num'];
        $params['book'] = serialize($params['book']);
        $meeting_book = $this->StringMerging($params['meeting_book'],3);
        $params['meeting_book_num'] = $meeting_book['num'];
        $params['meeting_book'] = serialize($params['meeting_book']);
        $school_book = $this->StringMerging($params['school_book'],3);
        $params['school_book_num'] = $school_book['num'];
        $params['school_book'] = serialize($params['school_book']);
        $params['region'] = $region;
        $params['userid'] = $user['userid'];
        $params['department_id'] = $user['department'];
        $params['name'] = $user['department_text'];
        $params['uptime'] = $upTime;
        $params['endtime'] = $endTime;
        $sale = Sale::create($params,array_keys($params));
        $book_total = '';
        if($params['book_total'])
        {
            $book_total = "会场预订：{$params['book_total']}套\n";
        }
        $office_total = '';
        if($params['office_total'])
        {
            $office_total = "办公室预订：{$params['office_total']}套\n";
        }
        $metting_num = '';
        if($params['metting_num'])
        {
            $metting_num = "会场来人：{$params['metting_num']}人";
        }
        $hot_num = '';
        if($params['hot_num'])
        {
            $hot_num = "暴场家庭：{$params['hot_num']}人";
        }
        $msg =  "{$params['ronda']}\n{$video['msg']}{$book_total}{$office_total}合计：{$video['num']}套\n{$book['msg']}合计：{$book['num']}元\n{$meeting_book['msg']}{$school_book['msg']}{$metting_num}\n{$hot_num}\n讲师：{$params['lecturer']} ";
        return ['sale'=>$sale,'msg'=>$msg];
    }

    /**
     * 字符串拼接
     * @param array $video
     * @param int $tab
     * @return array
     */
    public function StringMerging(array $video,$tab = 1)
    {
        $num  = 0;
        $msg = '';
        foreach ($video as $index => $item) {
            $num += $item['num'];
            if($tab == 1) //提分宝典
            {
                $msg .= sprintf("%u元: %s套\n",$item['style'],$item['num']);
            }
            if($tab == 2) //一轮书
            {
                $msg .= sprintf("%s: %s元\n",$item['style'],$item['num']);
            }
            if($tab == 3) //赠书
            {
                $msg .= sprintf("%s: %s本\n",$item['style'],$item['num']);
            }
        }
        return ['num'=>$num,'msg'=>$msg];
    }

}
